package Chapter2.Comparing.c6;

import java.util.*;

public class SetDemo1 {
    public static void main(String[] args) {
        Set<User> userSet = new TreeSet<>(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {

                    //年龄
                    int num = o1.getAge()-o2.getAge();
                    //按照姓名的内容
                    int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
                    //按照姓名的长度
                    int num3 = num2 == 0 ? o1.getName().length() - o2.getName().length() : num2;
                    return num3;

                }

        });
        //默认使用的是自然排序
        User u1 = new User("Jim",21);
        User u2 = new User("James",6);
        User u5= new User("Jame",6);
        User u3 = new User("Jack",20);
        User u4 = new User("Jim",21);

        userSet.add(u1);
        userSet.add(u2);
        userSet.add(u3);
        userSet.add(u4);
        userSet.add(u5);

        for(User u:userSet) {
            System.out.println(u);
        }

    }
}
